package com.cskaoyan.wordmemorize.controller.admin;

import com.cskaoyan.wordmemorize.auth.SaAdminCheckPermission;
import com.cskaoyan.wordmemorize.common.result.Result;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.dto.admin.UserVocAISituationDTO;
import com.cskaoyan.wordmemorize.request.BatchDeleteCommand;
import com.cskaoyan.wordmemorize.request.PageRequest;
import com.cskaoyan.wordmemorize.request.UserVocAISituationCommand;
import com.cskaoyan.wordmemorize.service.AIReadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/admin/voc/ai-read")
public class AdminVocAIReadController {

    @Autowired
    AIReadService aiReadService;

    @GetMapping("situation/list")
    @SaAdminCheckPermission("admin:ai-read:situation:search")
    public Result<PageDTO<UserVocAISituationDTO>> getSituationList(PageRequest pageRequest){

        PageDTO<UserVocAISituationDTO> aiBookSituatiions = aiReadService.getAISituationList(pageRequest);

        return Result.ok(aiBookSituatiions);
    }

    @PutMapping("situation")
    @SaAdminCheckPermission("admin:ai-read:situation:update")
    public Result updateSituation(@RequestBody UserVocAISituationCommand userVocAISituationCommand){
        aiReadService.updateAISituation(userVocAISituationCommand);
        return Result.ok();
    }

    @PostMapping("situation")
//    @SaAdminCheckPermission("admin:ai-read:situation:add")
    public Result addSituation(@RequestBody UserVocAISituationCommand userVocAISituationCommand){
        aiReadService.addAISituation(userVocAISituationCommand);
        return Result.ok();
    }

    @DeleteMapping("situation/{id}")
    @SaAdminCheckPermission("admin:ai-read:situation:delete")
    public Result delSituationById(@PathVariable("id")Long id){
        aiReadService.deleteAISituation(id);
        return Result.ok();
    }

    @DeleteMapping("situation")
    @SaAdminCheckPermission("admin:ai-read:situation:batchdelete")
    public Result delSituations(@RequestBody BatchDeleteCommand batchDeleteCommand){
        List<Long> situations = new ArrayList<>();
        for (String id : batchDeleteCommand.getIds()) {
            situations.add(Long.parseLong(id));
        }
        aiReadService.batchDeleteAISituationOption(situations);
        return Result.ok();
    }

    @DeleteMapping("option/{id}")
    public Result delOption(@PathVariable("id")Long id){
        aiReadService.deleteAISituationOption(id);
        return Result.ok();
    }

}
